package com.sfeir.tv.client.ui.search;

import com.google.gwt.user.client.ui.Widget;
import com.sfeir.tv.client.mvc.framework.AppEvent;
import com.sfeir.tv.client.mvc.framework.AppEventListener;
import com.sfeir.tv.client.mvc.framework.Model;

public class ListModel extends Model {

	protected ItemModel selectedItem = null;
	public ListModel() {
		super();
	}
	
    public void post()
    {
        registerListeners();
        super.post();
    }

	private void registerListeners()
    {
        register( AppEvent.CLICK_EVENT, new AppEventListener()
        {
            public void handleAppEvent( AppEvent event )
            {
                selectDeselectWidgets( event );
            }
        });
    }

    private void selectDeselectWidgets( AppEvent event )
	{
        Model m = (Model) event.getPayload();
        int index = ((ListView)view).getWidgetIndex( (Widget) m.getView() );
        if (index != -1)
        {
            if (selectedItem != null) ((ListView)view).deselectWidget(((ListView)view).getWidgetIndex((Widget) selectedItem.getView()));
            ((ListView)view).selectWidget(index);
            selectedItem = (ItemModel) m;
        }
	}
}